//
//  CustomAlertView.m
//  Behing
//
//  Created by Lj on 2017/10/9.
//  Copyright © 2017年 lj. All rights reserved.
//

#define ButtonTag 12331

#import "CustomAlertView.h"

@interface CustomAlertView()
@property (nonatomic, strong) UIView *subBackView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *describeLabel;
@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) NSString *titleStr;
@property (nonatomic, strong) NSString *describeStr;
@property (nonatomic, assign) id <BaseProtocol> delegate;
@property (nonatomic, assign) CustomAlertViewType viewType;

@end

@implementation CustomAlertView

- (UILabel *)describeLabel {
    if (_describeLabel == nil) {
        _describeLabel = [[UILabel alloc]init];
        _describeLabel.numberOfLines = 3;
        _describeLabel.adjustsFontSizeToFitWidth = YES;
        _describeLabel.font = BHFont_Size_17;
        _describeLabel.textColor = Text_Color_999999;
        [self.backView addSubview:_describeLabel];
    }
    return _describeLabel;
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = Text_Color_999999;
        _titleLabel.font = BHFont_Size_22;
        [self.backView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (NSMutableArray *)buttonArray {
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

- (id _Nonnull )initWithTitle:(NSString *)title message:(NSString *_Nonnull)message viewType:(CustomAlertViewType)viewType delegate:(id _Nonnull )delegate cancelButtonTitle:(NSString *_Nonnull)cancelButtonTitle otherButtonTitles:(NSString *_Nonnull)otherButtonTitles {
    if (![BHSettingUtil dataAndStringIsNull:cancelButtonTitle]) {
        [self.buttonArray addObject:cancelButtonTitle];
    }
    if (![BHSettingUtil dataAndStringIsNull:otherButtonTitles]) {
        [self.buttonArray addObject:otherButtonTitles];
    }
    self.viewType = viewType;
    self.titleStr = title;
    self.describeStr = message;
    self.delegate = delegate;
    if (self = [self init]) {
    }
    return self;
}

- (id)init {
    if (self = [super init]) {
        [self creatView];
    }
    return self;
}

- (void)creatView {
    _subBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height)];
    _subBackView.backgroundColor = [UIColor blackColor];
    _subBackView.alpha = 0;
    [BHSettingUtil addTapGestureInView:_subBackView addTarget:self action:@selector(backClick) withTag:23132];
    self.frame = _subBackView.bounds;
    [self addSubview:_subBackView];
    
    UIView *backView = [[UIView alloc]init];
    backView.center = CGPointMake(App_Frame_Width/2, App_Frame_Height/2);
    backView.bounds = CGRectMake(0, 0, App_Frame_Width - 60,self.viewType == CustomAlertViewTypeNone? 133 : 178);
    backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backView];
    _backView = backView;
    
    [self setUpUI];
 }

- (void)setUpUI {
    self.describeLabel.text = self.describeStr;
    if (self.viewType == CustomAlertViewTypeNone) {
        self.describeLabel.textAlignment = NSTextAlignmentLeft;
        [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(30);
            make.width.offset(CGRectGetWidth(self.backView.bounds) - 60);
            make.top.equalTo(self.backView).offset(30);
        }];
    }else {
        self.titleLabel.text = self.titleStr;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.describeLabel.textAlignment = NSTextAlignmentCenter;
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.backView).offset(20);
            make.width.offset(CGRectGetWidth(self.backView.bounds) - 60);
            make.left.equalTo(self.backView).offset(30);
        }];
        
        [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(30);
            make.width.offset(CGRectGetWidth(self.backView.bounds) - 60);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        }];
    }
    
    if (self.buttonArray.count == 1 && self.viewType == CustomAlertViewTypeNone) {
        return;
    }
    
    for (int i = 0; i < self.buttonArray.count; i ++) {
        UIButton *button = [[UIButton alloc]init];
        button.tag = ButtonTag + i;
        [button setTitle:self.buttonArray[i] forState:UIControlStateNormal];
        if (self.viewType == CustomAlertViewTypeNone) {
            [button setTitleColor:Main_Color forState:UIControlStateNormal];
            button.titleLabel.font = BHFont_Size_17;
        }else {
            if (self.buttonArray.count == 1) {
                button.titleLabel.font = BHFont_Size_17;
                [button setTitleColor:Main_Color forState:UIControlStateNormal];
                button.layer.cornerRadius = 22.5;
                button.layer.borderWidth = BHBorder_Width * 2;
                button.layer.borderColor = Main_Color.CGColor;
            }else {
                button.titleLabel.font = BHFont_Size_15;
                if (i == 0) {
                    [button setTitleColor:Text_Color_999999 forState:UIControlStateNormal];
                    button.backgroundColor = [UIColor whiteColor];
                }else {
                    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    button.backgroundColor = Main_Color;
                }
            }
        }
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.viewType == CustomAlertViewTypeNone) {
                make.bottom.equalTo(self.backView.mas_bottom).offset(- 15);
                make.right.equalTo(self.backView.mas_right).offset(-75 * i);
                make.width.offset(75);
            }else {
                if (self.buttonArray.count == 1) {
                    make.height.offset(45);
                    make.width.offset(190);
                    make.centerX.equalTo(self.backView);
                    make.bottom.equalTo(self.backView.mas_bottom).offset(- 15);
                }else {
                    make.bottom.equalTo(self.backView.mas_bottom).offset(0);
                    make.left.equalTo(self.backView).offset(CGRectGetWidth(self.backView.bounds)/2 * i);
                    make.width.offset(CGRectGetWidth(self.backView.bounds)/2);
                    make.height.offset(45);
                }
            }
        }];
    }
}

#pragma mark - show
- (UIView *)topView {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    return window;
}

- (void)presentContentView:(UIView *)view animated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.5 animations:^{
            self.subBackView.alpha = 0.5;
            self.backView.alpha = 1;
        } completion:^(BOOL finished) {
        }];
        [[self topView]addSubview:self];
        [self showTransformAnimation];
    }else {
        _subBackView.alpha = 0.5;
        _backView.alpha = 1;
        [[self topView]addSubview:self];
    }
}

#pragma mark - Animation
//transform
- (void)showTransformAnimation {
    CAKeyframeAnimation *alertViewShowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    alertViewShowAnimation.duration = 0.5;
    alertViewShowAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DIdentity]];
    alertViewShowAnimation.keyTimes = @[@0.2f,@0.5f,@0.75f,@1.0f];
    alertViewShowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [_backView.layer addAnimation:alertViewShowAnimation forKey:nil];
}

#pragma mark - 取消视图
- (void)cancelView {
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.alpha = 0.0;
        self.subBackView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.buttonArray removeAllObjects];
        self.backView = nil;
        self.subBackView = nil;
    }];
}

#pragma mark - 按钮
//背景点击事件
- (void)backClick {
    [self cancelView];
}

- (void)buttonClick:(UIButton *)sender {
    [self cancelView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeColumnView:didSelect:)]) {
        [self.delegate homeColumnView:self didSelect:sender.tag - ButtonTag];
    }
    self.ButtonClick == nil ? : self.ButtonClick(sender.tag - ButtonTag);
}

@end
